import Vue from 'vue'
import Router from 'vue-router'
import { getValue } from '@/libs/util'
Vue.use(Router)

const routes = [
  {
    path:'/',           //这个表示的是根目录，即一进入的页面
    redirect:'login'    //设置页面一进来就显示的页面，即重定向到login组件，这里写的内容是对应组将的component的值
  },
  {
    path: '/login',
    name: 'login',
    component: require('@/view/login/login.vue')
  },
  {
    path: '/chat',
    name: 'chat',
    component: require('@/view/chat/chat.vue')
  },
  {
    path: '/friend',
    name: 'friend',
    component: require('@/view/friend/friend.vue')
  }
]

const router = new Router({
  routes,
  linkActiveClass: 'active',
  mode: 'hash'
})
router.beforeEach((to, from, next) => {
  const token = getValue('loginInfo').token
  if (!token && to.name !== 'login') { 
    next({
      name: 'login' // 跳转到登录页
    })
  }else if(token && to.name == 'login') {
    next({
      name: 'chat' // 跳转到聊天页
    })
  }else {
    next()
  }
})

export default router
